﻿
Imports std
Imports store = cacheManager(Of String, bytes)

Namespace storageJobConsumerInternal
    Public Class cacheslice
        Inherits memslice

        Private c As store = Nothing

        Public Shared Shadows Function initial(ByVal name As String,
                                               ByVal config As configParser,
                                               ByVal c As cacheslice) As Boolean
            If memslice.initial(name, config, c) Then
                c.c = New store(False, c.maxKeyCount(), store.notRetireTicks)
                Return True
            Else
                Return False
            End If
        End Function

        Protected Overrides Function delete(ByVal key As String) As Boolean
            Return c.erase(key)
        End Function

        Protected Overrides Function keycount() As Int64
            Return c.size()
        End Function

        Protected Overrides Function list() As vector(Of String)
            Dim i As vector(Of String) = Nothing
            i = New vector(Of String)()
            If c.traversal(Sub(ByRef s As String, ByRef b As bytes)
                               assert(i.push_back(s))
                           End Sub) Then
                Return i
            Else
                Return Nothing
            End If
        End Function

        Protected Overrides Function modify(ByVal key As String, ByVal buff As bytes) As Boolean
            c.add(key, buff)
            Return True
        End Function

        Protected Overrides Function read(ByVal key As String) As bytes
            Return c.get(key)
        End Function

        Protected Overrides Function retire() As Boolean
            c.clear()
            Return True
        End Function

        Protected Overrides Function seek(ByVal key As String) As Boolean
            Return c.have(key)
        End Function

        Protected Overrides Function sizeof(ByVal key As String) As Int64
            Dim b As bytes = Nothing
            If c.get(key, b) Then
                Return b.size()
            Else
                Return npos
            End If
        End Function

        Protected Overrides Function valuesize() As Int64
            Dim i As Int64 = 0
            If c.traversal(Sub(ByRef s As String, ByRef b As bytes)
                               If Not b Is Nothing Then
                                   i += b.size()
                               End If
                           End Sub) Then
                Return i
            Else
                Return npos
            End If
        End Function

        Protected Overrides Function write(ByVal key As String, ByVal buff As bytes) As Boolean
            Dim b As bytes = Nothing
            If c.get(key, b) Then
                b += buff
            Else
                b = buff
            End If
            c.add(key, b)
            Return True
        End Function
    End Class
End Namespace
